package com.ys.malini.web.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.ys.malini.dao.IUserDao;

import com.ys.malini.model.TUser;
import com.ys.malini.util.StringExtUtils;

@SuppressWarnings("unchecked")
public class LoginRegisterAction extends DispatchAction {

	/**
	 *  用户操作类
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws java.lang.Exception
	 */
	IUserDao userDao;
	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}
	public IUserDao getUserDao() {
		return userDao;
	}

	public LoginRegisterAction() {
		super();
	}
	
	public ActionForward login(ActionMapping mapping, ActionForm form,HttpServletRequest request,
			HttpServletResponse response)throws Exception {
		request.getSession().removeAttribute("user");
		
		String loginPhone = request.getParameter("loginPhone");
		String loginPwd = request.getParameter("loginPwd");
		String userType = request.getParameter("userType");
		
		TUser user = (TUser) userDao.findUserByUserPhone(loginPhone);
		if(user==null){
			request.setAttribute("loginMsg", "用户不存在");
			return mapping.findForward("loginFail");
		}else{
			if(!(StringExtUtils.hash(loginPwd)).equals(user.getUserPwd())){
				request.setAttribute("loginMsg", "密码错误");
				return mapping.findForward("loginFail");
			}
		}
		request.getSession().setAttribute("user", user);
		return mapping.findForward("commonLoginSuccess");
/*		if("0".equals(user.getUserType())){
			return mapping.findForward("commonLoginSuccess");	
		}else{
			return mapping.findForward("adminLoginSuccess");
		}*/
	}
	
	public ActionForward register(ActionMapping mapping, ActionForm form,HttpServletRequest request,
			HttpServletResponse response)throws Exception {
		String registerPhone = request.getParameter("registerPhone");
		String registerName = request.getParameter("registerName");
		String registerPwd = request.getParameter("registerPwd");
		String repeatPwd = request.getParameter("repeatPwd");
	//	int userType = Integer.parseInt(request.getParameter("userType"));
		
		if(registerPwd!=null&&!"".equals(registerPwd)&&repeatPwd!=null&&!"".equals(repeatPwd)){
			if(!registerPwd.equals(repeatPwd)){
				request.setAttribute("regsiterMsg", "两次输入的密码不一致");
				return mapping.findForward("registerFail");
			}
		}else{
			request.setAttribute("regsiterMsg", "密码不能为空");
			return mapping.findForward("registerFail");
		}
		TUser user = (TUser) userDao.findUserByUserPhone(registerPhone);
		if(user==null){
			user = new TUser();
			user.setUserPhone(registerPhone);
			user.setUserName(registerName);
			user.setUserPwd(StringExtUtils.hash(registerPwd));
			user.setUserType("0");
		}else{
			request.setAttribute("regsiterMsg", "你所输入的手机号码已注册");
			return mapping.findForward("registerFail");
		}
		userDao.saveNewUser(user);
		request.setAttribute("regsiterMsg", "注册成功");
		return mapping.findForward("registerSuccess");
	}
	
	public ActionForward logout(ActionMapping mapping, ActionForm form,HttpServletRequest request,
			HttpServletResponse response)throws Exception {
		request.getSession().removeAttribute("user");
		return mapping.findForward("index");
	}
}
